package com.shenghuai.bclient.stores.adapter;

import android.content.Context;

import androidx.annotation.IntRange;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;


import java.util.List;

public abstract class RecyclerBaseAdapter<T> extends RecyclerView.Adapter<ViewHolder> implements IRecyclerAdapter<T> {

    private static final String TAG = RecyclerBaseAdapter.class.getSimpleName();

    private List<T> mDataList;
    private Context mContext;

    protected RecyclerBaseAdapter(@NonNull Context context, @NonNull List<T> mDataList) {
        this.mDataList = mDataList;
        this.mContext = context;
    }

    public void release() {
        mContext = null;
    }

    /*@Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position, @NonNull List<Object> payloads) {
        onBindViewHolder(holder, position, payloads);
    }*/

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        final int p = holder.getBindingAdapterPosition();
        bindDataForView(holder, getItem(p), p);
    }

    @Override
    public int getItemCount() {
        return mDataList != null ? mDataList.size() : 0;
    }

    protected abstract void bindDataForView(ViewHolder holder, T t, int position);

    @Override
    public Context getContext() {
        return mContext;
    }

    @Override
    public T getItem(@IntRange(from = 0) int position) {
        if (position <= -1) {
            return null;
        }
        if (mDataList == null || mDataList.isEmpty())
            return null;
        if (position < mDataList.size())
            return mDataList.get(position);
        return null;
    }

    @Override
    public List<T> getDataList() {
        return mDataList;
    }

    public void setDataList(List<T> mDataList) {
        this.mDataList = mDataList;
    }

    //From a certain position, insert a set of data
    @Override
    public void insertItems(@NonNull List<T> list, @IntRange(from = 0) int startIndex) {

        if (mDataList == null) {
            return;
        }

        if (list.isEmpty()) {
            return;
        }

        if (this.mDataList.containsAll(list)) {
            return;
        }

        mDataList.addAll(startIndex, list);
        notifyItemRangeInserted(startIndex, list.size());
        //notifyItemRangeChanged(startIndex, getItemCount() - startIndex);
    }

    //Inserting a set of data from the bottom
    @Override
    public void insertItems(@NonNull List<T> list) {
        this.insertItems(list, mDataList.size());
    }

    //From a certain position, insert a data
    @Override
    public void insertItem(@NonNull T t, @IntRange(from = 0) int position) {

        if (mDataList == null) {
            return;
        }

        if (t == null) {
            return;
        }

//        if (this.mDataList.contains(t)) {
//            return;
//        }

        notifyItemInserted(position);
        mDataList.add(position, t);
        notifyItemRangeChanged(position, getItemCount() - position);
    }

    //Inserting a data from the bottom
    @Override
    public void insertItem(@NonNull T t) {
        this.insertItem(t, mDataList.size());
    }

    //Replace all data
    @SuppressWarnings("ConstantConditions")
    @Override
    public void replaceData(@NonNull List<T> list) {
        if (mDataList == null) {
            return;
        }

        if (list == null) {
            return;
        }

        mDataList = list;
        notifyDataSetChanged();
    }

    //From a certain location, update n data
    @Override
    public void updateItems(@IntRange(from = 0) int positionStart, @IntRange(from = 0) int itemCount) {
        notifyItemRangeChanged(positionStart, itemCount);
    }

    //Update all data
    @Override
    public void updateAll() {
        updateItems(0, mDataList.size());
    }

    //Remove data from a location
    @Override
    public void removeItem(@IntRange(from = 0) int position) {
        if (mDataList == null || mDataList.isEmpty() || position <= -1) {
            return;
        }
        notifyItemRemoved(position);
        mDataList.remove(position);
        notifyItemRangeChanged(position, getItemCount() - position);
    }

    //Remove all data
    @Override
    public void removeAll() {

        if (mDataList == null || mDataList.isEmpty()) {
            return;
        }

        mDataList.clear();
        notifyDataSetChanged();
    }
}
